package com.mysoft.gfyx.common.rpc.opptyservice.impl;

import com.mysoft.framework.context.api.ContextManager;
import com.mysoft.gfyx.common.concurrency.CacheAgentService;
import com.mysoft.gfyx.common.rpc.opptyservice.OpptyServiceClientService;
import com.mysoft.gfyx.common.rpc.opptyservice.OpptyServiceClientFactoryService;
import feign.Feign;
import feign.jackson.JacksonDecoder;
import feign.jackson.JacksonEncoder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
public class OpptyServiceClientFactoryServiceImpl implements OpptyServiceClientFactoryService {

    @Value("${oppty-service.host}")
    private String host;

    @Value("${oppty-service.app-code}")
    private String appCode;

    @Value("${oppty-service.iss}")
    private String iss;

    @Value("${oppty-service.secret}")
    private String secret;

    @Autowired
    private CacheAgentService cacheAgentService;

    @Autowired
    private ContextManager contextManager;

    @Override
    public OpptyServiceClientService getOpptyServiceClientService() {
        OpptyServiceClientService service = Feign.builder()
                .encoder(new JacksonEncoder())
                .decoder(new JacksonDecoder())
                .requestInterceptor((new OpptyServiceRequestInterceptorImpl(host, appCode, iss, secret, cacheAgentService, contextManager)))
                .target(OpptyServiceClientService.class, host);

        return service;
    }
}
